﻿/****************************** Module Header ******************************\
 * Module Name:  DbParams.cs
 * Module Version:  1.0.0 (2012-9-4)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to list the file type.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;

namespace UCF.Core.Data
{
    /// <summary>
    /// 数据库参数对象
    /// <para>提供存储过程参数的相关方法</para>
    /// <para>DbParams.New(DBHelper.GetDbCommand(providerName)).Add&lt;int&gt;("intOutCode").AddRange&lt;string&gt;(new string[] { "@loginid", "@password", "@ip" }, loginid, password, ip).AddRange&lt;int&gt;(new string[] { "type", "code" }, 5, 6).ToArrayDbParameters()</para>
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class DbParams
    {
        private static DbParams instance;

        /// <summary>
        /// 参数对象列表
        /// </summary>
        private IList<DbParameter> lstParams;
        /// <summary>
        /// 通用数据库命令对象
        /// </summary>
        private DbCommand cmd;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="providerName"></param>
        public DbParams(string providerName)
        {
            cmd = DbProviderFactories.GetFactory(providerName).CreateCommand();
            lstParams = new List<DbParameter>();
        }
        /// <summary>
        /// 创建一个新对象
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public static DbParams New(string providerName)
        {
            instance = new DbParams(providerName);
            return instance;
        }

        /// <summary>
        /// 重置实例对象，清空所有参数对象
        /// </summary>
        public void Clear()
        {
            this.lstParams.Clear();
        }

        /// <summary>
        /// 添加一个输入参数
        /// </summary>
        /// <typeparam name="T">参数类型</typeparam>
        /// <param name="parameterName">参数名称</param>
        /// <param name="value">值</param>
        /// <param name="size">值的长度</param>
        /// <param name="direct">参数方向</param>
        public DbParams Add<T>(string parameterName, T value, int size, ParameterDirection direct)
        {
            this.lstParams.Add(this.NewParameter<T>(parameterName, value, size, direct));
            return instance;
        }

        /// <summary>
        /// 添加一个输入参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="size"></param>
        public DbParams Add<T>(string parameterName, T value, int size)
        {
            this.Add<T>(parameterName, value, size, ParameterDirection.Input);
            return instance;
        }
        /// <summary>
        /// 添加一个输入参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        public DbParams Add<T>(string parameterName, T value)
        {
            this.Add<T>(parameterName, value, 0, ParameterDirection.Input);
            return instance;
        }

        /// <summary>
        /// 添加一个输出参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        public DbParams Add<T>(string parameterName)
        {
            this.lstParams.Add(this.NewParameter<T>(parameterName));
            return instance;
        }
        /// <summary>
        /// 批量添加输入参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterNames"></param>
        /// <param name="values"></param>
        public DbParams AddRange<T>(string[] parameterNames, params T[] values)
        {
            int i = 0;
            foreach (string paraName in parameterNames)
            {
                this.Add<T>(paraName, values[i]);
                i++;
            }
            return instance;
        }

        /// <summary>
        /// 批量添加输出参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterNames"></param>
        public DbParams AddRange<T>(string[] parameterNames)
        {
            int i = 0;
            foreach (string paraName in parameterNames)
            {
                this.Add<T>(paraName);
                i++;
            }
            return instance;
        }

        /// <summary>
        /// 获取一个参数对象实例，参数方向是输入的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="size"></param>
        /// <param name="direct"></param>
        /// <returns></returns>
        public DbParameter NewParameter<T>(string parameterName, T value, int size, ParameterDirection direct)
        {
            DbParameter para = this.cmd.CreateParameter();
            para.ParameterName = parameterName;
            Type type = value.GetType();
            para.DbType = DatabaseHelper.ClrTypeToDbType(type);
            para.Value = value;
            if (size > 0) para.Size = size;
            para.Direction = direct;
            return para;
        }
        /// <summary>
        /// 获取一个参数对象实例，参数方向是输入的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public DbParameter NewParameter<T>(string parameterName, T value, int size)
        {
            return this.NewParameter(parameterName, value, size, ParameterDirection.Input);
        }
        /// <summary>
        /// 获取一个参数对象实例，参数方向是输入的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public DbParameter NewParameter<T>(string parameterName, T value)
        {
            return this.NewParameter(parameterName, value, 0);
        }
        /// <summary>
        /// 获取一个参数对象实例，参数方向是输出的
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public DbParameter NewParameter<T>(string parameterName)
        {
            DbParameter para = this.cmd.CreateParameter();
            para.ParameterName = parameterName;
            Type type = typeof(T);
            para.DbType = DatabaseHelper.ClrTypeToDbType(type);
            if (type == typeof(string) || type == typeof(DateTime)) para.Size = 255;
            para.Direction = ParameterDirection.Output;
            return para;
        }

        /// <summary>
        /// 获取DbParameter格式的数组
        /// </summary>
        /// <returns></returns>
        public DbParameter[] ToArray()
        {
            int intCount = this.lstParams.Count;
            DbParameter[] arrPara = new DbParameter[intCount];
            int i = 0;
            foreach (DbParameter para in this.lstParams)
            {
                arrPara[i] = para;
                i++;
            }
            return arrPara;
        }

        /// <summary>
        /// 获取DbParameter格式的数组
        /// </summary>
        /// <returns></returns>
        public DbParameter[] ToArrayDbParameters()
        {
            int intCount = this.lstParams.Count;
            DbParameter[] arrPara = new DbParameter[intCount];
            int i = 0;
            foreach (DbParameter para in this.lstParams)
            {
                arrPara[i] = para;
                i++;
            }
            return arrPara;
        }
    }
}
